<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>vuepress-mergeable | VuePress Community</title>
    <meta name="description" content="Community supported ecosystem for VuePress">
    <link rel="icon" href="/logo/600x600.png">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.bde909c2.css" as="style"><link rel="preload" href="/assets/js/app.83cb924b.js" as="script"><link rel="preload" href="/assets/js/2.96e68d5e.js" as="script"><link rel="preload" href="/assets/js/23.14ac2e9b.js" as="script"><link rel="preload" href="/assets/js/5.1cd3ace7.js" as="script"><link rel="prefetch" href="/assets/js/10.8fab5e9f.js"><link rel="prefetch" href="/assets/js/11.9e66637f.js"><link rel="prefetch" href="/assets/js/12.23a476a6.js"><link rel="prefetch" href="/assets/js/13.cbd5d914.js"><link rel="prefetch" href="/assets/js/14.78046e0f.js"><link rel="prefetch" href="/assets/js/15.15f2d0bd.js"><link rel="prefetch" href="/assets/js/16.c749a5c9.js"><link rel="prefetch" href="/assets/js/17.f54b2102.js"><link rel="prefetch" href="/assets/js/18.0da93583.js"><link rel="prefetch" href="/assets/js/19.7ec782b6.js"><link rel="prefetch" href="/assets/js/20.2ac16953.js"><link rel="prefetch" href="/assets/js/21.059dc84f.js"><link rel="prefetch" href="/assets/js/22.b80a1088.js"><link rel="prefetch" href="/assets/js/24.645912bb.js"><link rel="prefetch" href="/assets/js/25.d99ab9d1.js"><link rel="prefetch" href="/assets/js/26.439ac2ad.js"><link rel="prefetch" href="/assets/js/27.7a5265ba.js"><link rel="prefetch" href="/assets/js/28.68a7bdf2.js"><link rel="prefetch" href="/assets/js/29.83d3986f.js"><link rel="prefetch" href="/assets/js/3.e29c89cb.js"><link rel="prefetch" href="/assets/js/30.44036763.js"><link rel="prefetch" href="/assets/js/31.4e32f3c0.js"><link rel="prefetch" href="/assets/js/32.5c64f4c8.js"><link rel="prefetch" href="/assets/js/33.bc6170ff.js"><link rel="prefetch" href="/assets/js/34.2a1877c0.js"><link rel="prefetch" href="/assets/js/35.6a0693b1.js"><link rel="prefetch" href="/assets/js/36.036cfdd9.js"><link rel="prefetch" href="/assets/js/37.5c4deb98.js"><link rel="prefetch" href="/assets/js/4.a98f17db.js"><link rel="prefetch" href="/assets/js/6.b1866fbb.js"><link rel="prefetch" href="/assets/js/7.757f3a25.js"><link rel="prefetch" href="/assets/js/8.e5910e9d.js"><link rel="prefetch" href="/assets/js/9.68190a5d.js">
    <link rel="stylesheet" href="/assets/css/0.styles.bde909c2.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/en/" class="home-link router-link-active"><!----> <span class="site-name">VuePress Community</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/en/tools/mergeable/" class="nav-link router-link-exact-active router-link-active">English</a></li><li class="dropdown-item"><!----> <a href="/zh/tools/mergeable/" class="nav-link">简体中文</a></li></ul></div></div> <a href="https://github.com/vuepress/vuepress.github.io" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/en/tools/mergeable/" class="nav-link router-link-exact-active router-link-active">English</a></li><li class="dropdown-item"><!----> <a href="/zh/tools/mergeable/" class="nav-link">简体中文</a></li></ul></div></div> <a href="https://github.com/vuepress/vuepress.github.io" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Plugins</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Tools</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/en/tools/mergeable/" class="active sidebar-link">vuepress-mergeable</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/en/tools/mergeable/#why-do-i-need-this" class="sidebar-link">Why do I need this?</a></li><li class="sidebar-sub-header"><a href="/en/tools/mergeable/#deconstruct-merge" class="sidebar-link">Deconstruct Merge</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/en/tools/mergeable/#define-how-properties-are-merged-in-an-object" class="sidebar-link">Define how properties are merged in an object</a></li><li class="sidebar-sub-header"><a href="/en/tools/mergeable/#define-how-elements-are-merged-in-an-array" class="sidebar-link">Define how elements are merged in an array</a></li><li class="sidebar-sub-header"><a href="/en/tools/mergeable/#use-functions-to-customize-merge-strategies" class="sidebar-link">Use functions to customize merge strategies</a></li><li class="sidebar-sub-header"><a href="/en/tools/mergeable/#deconstruct-your-options" class="sidebar-link">Deconstruct your options</a></li></ul></li><li class="sidebar-sub-header"><a href="/en/tools/mergeable/#usage" class="sidebar-link">Usage</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/en/tools/mergeable/#mergeable-plugin-mergeoptions-defaultoptions-plugin" class="sidebar-link">mergeable(plugin, mergeOptions?, defaultOptions?): plugin</a></li></ul></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="vuepress-mergeable"><a href="#vuepress-mergeable" aria-hidden="true" class="header-anchor">#</a> vuepress-mergeable <a rel="noopener noreferrer" target="_blank" title="vuepress/vuepress-mergeable" href="https://github.com/vuepress/vuepress-mergeable" class="github-link"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 28 28"><path d="M14 0C6.27 0 0 6.43 0 14.36c0 6.34 4.01 11.72 9.57 13.62.7.13.96-.31.96-.69 0-.34-.01-1.24-.02-2.44-3.89.87-4.72-1.92-4.72-1.92-.64-1.66-1.55-2.1-1.55-2.1-1.27-.89.1-.87.1-.87 1.4.1 2.14 1.48 2.14 1.48 1.25 2.19 3.28 1.56 4.07 1.19.13-.93.49-1.56.89-1.92-3.11-.36-6.38-1.59-6.38-7.09 0-1.57.55-2.85 1.44-3.85-.14-.36-.62-1.82.14-3.8 0 0 1.18-.39 3.85 1.47a12.8 12.8 0 0 1 3.5-.48c1.19.01 2.39.16 3.5.48 2.67-1.86 3.85-1.47 3.85-1.47.76 1.98.28 3.44.14 3.8.9 1 1.44 2.28 1.44 3.85 0 5.51-3.27 6.73-6.39 7.08.5.44.95 1.32.95 2.66 0 1.92-.02 3.47-.02 3.94 0 .38.25.83.96.69C23.99 26.07 28 20.7 28 14.36 28 6.43 21.73 0 14 0z" fill-rule="evenodd" clip-rule="evenodd"></path></svg></a></h1> <p><code>vuepress-mergeable</code> is a tool for customizing how a VuePress plugin merges its options based on <a href="https://github.com/Shigma/deconstruct-merge" target="_blank" rel="noopener noreferrer">deconstruct-merge<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>.</p> <h2 id="why-do-i-need-this"><a href="#why-do-i-need-this" aria-hidden="true" class="header-anchor">#</a> Why do I need this?</h2> <p>In VuePress, a plugin can be used multiple times but only one <code>options</code> will be kept. If your plugins are used in different places (such as being directly referenced in <code>config.js</code> and indirectly used by plugins and themes), their <code>options</code> will overwrite each other, causing undetectable side effects. This tool can help you create a plugin that automatically merges <code>options</code> to your liking.</p> <h2 id="deconstruct-merge"><a href="#deconstruct-merge" aria-hidden="true" class="header-anchor">#</a> Deconstruct Merge</h2> <p>There are many ways to merge two options:</p> <ul><li><code>override</code>: the latter passed option overrides the previous one</li> <li><code>assign</code>: merge options with <code>Object.assign</code></li> <li><code>array</code>: put all incoming options into an array</li> <li><code>flat</code>: put all incoming options into a flattened array</li></ul> <h3 id="define-how-properties-are-merged-in-an-object"><a href="#define-how-properties-are-merged-in-an-object" aria-hidden="true" class="header-anchor">#</a> Define how properties are merged in an object</h3> <p>If an option is an object where different properties use different merge methods, you can also write the option itself as an object:</p> <div class="language-js extra-class"><pre class="language-js"><code>mergeOptions <span class="token operator">=</span> <span class="token punctuation">{</span>
  foo<span class="token punctuation">:</span> <span class="token string">'array'</span><span class="token punctuation">,</span>
  bar<span class="token punctuation">:</span> <span class="token string">'flat'</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><p>At a result, the <code>foo</code> option will be merged with arrays, and the <code>bar</code> option will be flattened.</p> <h3 id="define-how-elements-are-merged-in-an-array"><a href="#define-how-elements-are-merged-in-an-array" aria-hidden="true" class="header-anchor">#</a> Define how elements are merged in an array</h3> <p>If an option is an array where different elements are to be merged differently, you can also write the options themselves as an array:</p> <div class="language-js extra-class"><pre class="language-js"><code>mergeOptions <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token string">'override'</span><span class="token punctuation">,</span>
  <span class="token string">'assign'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span>
</code></pre></div><p>At a result, the first element of the array will use the override strategy, while the second element will be merged with <code>Object.assign</code>.</p> <h3 id="use-functions-to-customize-merge-strategies"><a href="#use-functions-to-customize-merge-strategies" aria-hidden="true" class="header-anchor">#</a> Use functions to customize merge strategies</h3> <p>If the above four merge strategies don't satisfy your needs, you can write a function to define the merge strategy:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function-variable function">mergeOptions</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> oldValue</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> value <span class="token operator">+</span> oldValue
<span class="token punctuation">}</span>
</code></pre></div><p>Of course, if you have a good idea, a PR is always welcomed.</p> <h3 id="deconstruct-your-options"><a href="#deconstruct-your-options" aria-hidden="true" class="header-anchor">#</a> Deconstruct your options</h3> <p>The syntax above can be applied recursively to your options, which allows you to fine-tune the merge logic for each detail. Here is a case:</p> <div class="language-js extra-class"><pre class="language-js"><code>mergeOptions <span class="token operator">=</span> <span class="token punctuation">{</span>
  foo<span class="token punctuation">:</span> <span class="token punctuation">[</span>
    <span class="token string">'override'</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token function-variable function">bar</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> oldValue</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> value <span class="token operator">+</span> oldValue<span class="token punctuation">,</span>
      baz<span class="token punctuation">:</span> <span class="token string">'flat'</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token string">'assign'</span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="tip custom-block"><p><code>override</code> is our default merge strategy. Therefore, any object properties or array elements that are not defined in <code>mergeOptions</code> will automatically take the override policy.</p></div> <h2 id="usage"><a href="#usage" aria-hidden="true" class="header-anchor">#</a> Usage</h2> <h3 id="mergeable-plugin-mergeoptions-defaultoptions-plugin"><a href="#mergeable-plugin-mergeoptions-defaultoptions-plugin" aria-hidden="true" class="header-anchor">#</a> mergeable(plugin, mergeOptions?, defaultOptions?): plugin</h3> <ul><li><strong>plugin:</strong> any VuePress plugin</li> <li><strong>mergeOptions:</strong> options for deconstruct merge</li> <li><strong>defaultOptions:</strong> default options for the plugin</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// vuepress-plugin-my-plugin/index.js</span>
<span class="token keyword">const</span> mergeable <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'vuepress-mergeable'</span><span class="token punctuation">)</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token function">mergeable</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">options</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token function">ready</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>options<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  <span class="token comment">// mergeOptions</span>
  foo<span class="token punctuation">:</span> <span class="token string">'assign'</span><span class="token punctuation">,</span>
  bar<span class="token punctuation">:</span> <span class="token string">'flat'</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  <span class="token comment">// defaultOptions</span>
  foo<span class="token punctuation">:</span> <span class="token punctuation">{</span> a<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// .vuepress/config.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span>
    <span class="token comment">// apply the plugin multiple times</span>
    <span class="token punctuation">[</span><span class="token string">'my-plugin'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
      foo<span class="token punctuation">:</span> <span class="token punctuation">{</span> b<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> c<span class="token punctuation">:</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      baz<span class="token punctuation">:</span> <span class="token string">'Hello'</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token string">'my-plugin'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
      foo<span class="token punctuation">:</span> <span class="token punctuation">{</span> b<span class="token punctuation">:</span> <span class="token number">3</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      bar<span class="token punctuation">:</span> <span class="token string">'Hello'</span><span class="token punctuation">,</span>
      baz<span class="token punctuation">:</span> <span class="token string">'World'</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><p>And this will be the merged options:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">{</span> foo<span class="token punctuation">:</span> <span class="token punctuation">{</span> a<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> b<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span> c<span class="token punctuation">:</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  bar<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'Hello'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  baz<span class="token punctuation">:</span> <span class="token string">'World'</span> <span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/vuepress/vuepress.github.io/edit/master/docs/en/tools/mergeable.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">4/3/2019, 10:46:03 AM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/en/plugins/zooming/" class="prev">
          vuepress-plugin-zooming
        </a></span> <!----></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.83cb924b.js" defer></script><script src="/assets/js/2.96e68d5e.js" defer></script><script src="/assets/js/23.14ac2e9b.js" defer></script><script src="/assets/js/5.1cd3ace7.js" defer></script>
  </body>
</html>
